var common = require('../../common.js');
var app = getApp();
Page({
	data: {
		isIphoneX: app.globalData.isIphoneX,
		size: 4,
		listData: [],
		pageMetaScrollTop: 0,
		scrollTop: 0,
		changed: false
	},
	sortEnd(e) {
		console.log("sortEnd", e.detail.listData)
		this.setData({
			listData: e.detail.listData,
			changed: true
		});
	},
	change(e) {
		console.log("change", e.detail.listData)
	},
	sizeChange(e) {
		wx.pageScrollTo({scrollTop: 0})
		this.setData({
			size: e.detail.value
		});
		this.drag.init();
	},
	itemClick(e) {
		console.log(e);
	},
	toggleFixed(e) {
		let key = e.currentTarget.dataset.key;
		let {listData} = this.data;
		listData[key].fixed = !listData[key].fixed
		this.setData({
			listData: listData
		});
		this.drag.init();
	},
	scroll(e) {
		this.setData({
			pageMetaScrollTop: e.detail.scrollTop
		})
	},
	// 页面滚动
	onPageScroll(e) {
		this.setData({
			scrollTop: e.scrollTop
		});
	},
	onLoad() {
		this.drag = this.selectComponent('#drag');

		var cates = wx.getStorageSync('cateList');
		if(!cates){
			wx.request({
        url: app.globalData.baseUrl + '/blade-system/application/init',
				data: {
					token: common.token,
					appId:wx.getAccountInfoSync().miniProgram.appId
				},
				method: 'POST',
				success: (res) => {
					cates = res.data.data.cates;
					this.setData({
						listData: cates
					});
					this.drag.init();
				}
			});
		}else{
			this.setData({
				listData: cates
			});
			this.drag.init();
		}
	},
	toBack:function(){
		wx.navigateBack({
			delta:2
		});
	},
	saveCate:function(){
		if(this.data.listData.length == 0){
			wx.showToast({
				title: '保存失败',
				icon: 'none'
			});
			return;
		}
		app.globalData.cateReresh = true;
		wx.setStorageSync('cateList', this.data.listData);
		wx.switchTab({
			url: '../index/index',
		});
	}
})
